package com.zoweunion.mechanic.controller.base;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.service.base.TuiHuanService;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.MyException;
@SuppressWarnings("unchecked")
@RestController
public class TuiHuanController extends BaseController{
	
	@Autowired
	private TuiHuanService tuiHuanService;
	
	//硬件申请退换
	@PostMapping("/applyTuiHuan")
	public ResponseBean applyTuiHuan(@RequestBody String str,HttpServletRequest request) throws MyException {
		logBefore(logger, "硬件申请退换");
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		reqMap.put("user", getCurrentUser(request).getUser_name());
		try {
			tuiHuanService.applyTuiHuan(reqMap);
		} catch (Exception e) {
			throw new MyException(Constants.CODE_HANDEL_FAILED, "业务受理失败");
		}
		return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", null);
	}

	@PostMapping("/serverInfo")
	public ResponseBean purchaseName(HttpServletRequest request) throws MyException {
		logBefore(logger, "服务商名称");
		Map<String, Object> reqMap = new HashMap<>();
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		Map<String,Object> result=new HashMap<>();
		try {
			result=tuiHuanService.purchaseName(reqMap);
		} catch (Exception e) {
			throw new MyException(Constants.CODE_HANDEL_FAILED, "业务受理失败");
		}
		return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);
	}

	//服务商申请设备采购
	@PostMapping("/applyPurchase")
	public ResponseBean applyPurchase(@RequestBody String str,HttpServletRequest request) throws MyException {
		logBefore(logger, "服务商申请设备采购");
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		reqMap.put("user", getCurrentUser(request).getUser_name());
		try {
			tuiHuanService.applyPurchase(reqMap);
		} catch (Exception e) {
			throw new MyException(Constants.CODE_HANDEL_FAILED, "业务受理失败");
		}
		return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", null);
	}
	
	//根据相关条件查询设备采购列表
	@PostMapping("/getPurchaseByCondition")
	public ResponseBean getPurchaseByCondition(@RequestBody String str,HttpServletRequest request) throws MyException {
		logBefore(logger, "根据相关条件查询设备采购列表");
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		Map<String, Object> resultList;
		try {
			resultList = tuiHuanService.getPurchaseByCondition(reqMap);
		} catch (Exception e) {
			throw new MyException(Constants.CODE_HANDEL_FAILED, "业务受理失败");
		}
		return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", resultList);
	}

	//根据相关条件查询设备采购列表

	@PostMapping("/getApplyTuiHuanByCondition")
	public ResponseBean getApplyTuiHuanByCondition(@RequestBody String str,HttpServletRequest request) throws MyException {
		logBefore(logger, "根据相关条件查询设备退换列表");
		Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
		reqMap.put("s_id", getCurrentUser(request).getS_id());
		Map<String, Object> resultList;
		try {
			resultList = tuiHuanService.getApplyTuiHuanByCondition(reqMap);
		} catch (Exception e) {
			throw new MyException(Constants.CODE_HANDEL_FAILED, "业务受理失败");
		}
		return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", resultList);
	}

}
